package com.qujia.customer.service.impl;

import java.beans.Transient;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.customer.domain.*;
import com.qujia.customer.mapper.CustomerCardUseRecordMapper;
import com.qujia.customer.mapper.CustomerElectronicCardMapper;
import com.qujia.customer.service.IIcRechargeChangeService;
import com.qujia.integral.domain.CustomerPointsRecord;
import com.qujia.system.domain.SysAppUserInfoUser;
import com.qujia.system.mapper.AppUserInfoUserMapper;
import com.qujia.system.service.IAppUserInfoUserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qujia.customer.mapper.IcRegisteInfoMapper;
import com.qujia.customer.service.IIcRegisteInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 卡编号Service业务层处理
 * 
 * @author qujia
 * @date 2021-11-13
 */
@Service
public class IcRegisteInfoServiceImpl implements IIcRegisteInfoService 
{
    @Autowired
    private IcRegisteInfoMapper icRegisteInfoMapper;


    @Autowired
    private AppUserInfoUserMapper  appUserInfoUserMapper;

    @Autowired
    private CustomerCardUseRecordMapper customerCardUseRecordMapper;

    @Autowired
    private CustomerElectronicCardMapper customerElectronicCardMapper;

    /**
     * 查询卡编号
     * 
     * @param icCardno 卡编号ID
     * @return 卡编号
     */
    @Override
    public IcRegisteInfo selectIcRegisteInfoById(String icCardno)
    {
        return icRegisteInfoMapper.selectIcRegisteInfoById(icCardno);
    }

    @Override
    public IcStatisticsInfo selectstatisticsInfo(IcRegisteInfo icRegisteInfo) {
        return icRegisteInfoMapper.selectstatisticsInfo(icRegisteInfo);
    }

    /**
     * 查询卡编号列表
     * 
     * @param icRegisteInfo 卡编号
     * @return 卡编号
     */
    @Override
    public List<IcRegisteInfo> selectIcRegisteInfoList(IcRegisteInfo icRegisteInfo)
    {
        return icRegisteInfoMapper.selectIcRegisteInfoList(icRegisteInfo);
    }

    /**
     * 新增卡编号
     * 
     * @param icRegisteInfo 卡编号
     * @return 结果
     */
    @Override
    public int insertIcRegisteInfo(IcRegisteInfo icRegisteInfo)
    {
        return icRegisteInfoMapper.insertIcRegisteInfo(icRegisteInfo);
    }

    /**
     * 修改卡编号
     * 
     * @param icRegisteInfo 卡编号
     * @return 结果
     */
    @Override
    public int updateIcRegisteInfo(IcRegisteInfo icRegisteInfo)
    {
        return icRegisteInfoMapper.updateIcRegisteInfo(icRegisteInfo);
    }

    @Override
    public int updatePasswd(IcRegisteInfo icRegisteInfo)
    {
        return icRegisteInfoMapper.updatePasswd(icRegisteInfo);
    }

    @Override
    public int updateRecharge(IcRegisteInfo icRegisteInfo)
    {
        return icRegisteInfoMapper.updateRecharge(icRegisteInfo);
    }

    @Override
    @Transactional
    public int updateRechargeTran(IcRegisteInfo icRegisteInfo)
    {

        createUserElectronic(icRegisteInfo.getIcCardno(),icRegisteInfo.getAmt(),icRegisteInfo.getStationId());

        return 1;
    }
    /***
     *
     * IcNo  卡编号
     * amt 充值金额，null为新建
     * stationId   若为充值则使用，非充值不必填
     *
     * */
    @Transactional
    @Override
    public Long createUserElectronic(String IcNo,BigDecimal amt,Long stationId)
    {
        IcRegisteInfo icRegisteInfo1=icRegisteInfoMapper.selectIcRegisteInfoById(IcNo);

        SysAppUserInfoUser sysAppUserInfoUser=new SysAppUserInfoUser();
        sysAppUserInfoUser.setMobilePhone(icRegisteInfo1.getIcContactsphone());
        sysAppUserInfoUser.setStationId(icRegisteInfo1.getEnterpriseId());
        List<SysAppUserInfoUser> sysAppUserInfoUsers=appUserInfoUserMapper.selectAppUserInfoList(sysAppUserInfoUser);
        if(sysAppUserInfoUsers!=null&&sysAppUserInfoUsers.size()>0){
            sysAppUserInfoUser=sysAppUserInfoUsers.get(0);
        }else{
            sysAppUserInfoUser.setRegisteDate(new Date());
            sysAppUserInfoUser.setMemberCode(new SimpleDateFormat("yyMMdd").format(new Date())+ RandomStringUtils.randomNumeric(5));
            sysAppUserInfoUser.setICCard(icRegisteInfo1.getIcCardno());
            appUserInfoUserMapper.insertAppUserInfo(sysAppUserInfoUser);
        }

        CustomerElectronicCard customerElectronicCard=new CustomerElectronicCard();
        customerElectronicCard.setCardOilsType(icRegisteInfo1.getCardType());
        customerElectronicCard.setStationId(icRegisteInfo1.getEnterpriseId());
        customerElectronicCard.setMobilePhone(icRegisteInfo1.getIcContactsphone());
        List<CustomerElectronicCard> customerElectronicCards=customerElectronicCardMapper.selectCustomerElectronicCardList(customerElectronicCard);

        if(customerElectronicCards!=null&&customerElectronicCards.size()>0){
            customerElectronicCard=customerElectronicCards.get(0);
            if(amt!=null) {
                customerElectronicCard.setAmt(customerElectronicCards.get(0).getAmt().add(amt));
                customerElectronicCardMapper.updateCustomerElectronicCard(customerElectronicCard);
            }
        }else {
            customerElectronicCard.setMobilePhone(icRegisteInfo1.getIcContactsphone());
            customerElectronicCard.setCustomerNo(sysAppUserInfoUser.getMemberCode());
            customerElectronicCard.setStationId(icRegisteInfo1.getEnterpriseId());
            if(amt!=null) {
                customerElectronicCard.setAmt(amt);
            }else {
                customerElectronicCard.setAmt(BigDecimal.ZERO);
            }
            customerElectronicCard.setCreateTime(new Date());
            customerElectronicCardMapper.insertCustomerElectronicCard(customerElectronicCard);
        }
       if(amt !=null) {
           CustomerCardUseRecord customerCardUseRecord = new CustomerCardUseRecord();
           customerCardUseRecord.setEnterpriseId(icRegisteInfo1.getEnterpriseId());
           customerCardUseRecord.setStatus("1");
           customerCardUseRecord.setPayType("stk");
           customerCardUseRecord.setUsageType("+");
           customerCardUseRecord.setMobilePhone(icRegisteInfo1.getIcContactsphone());
           customerCardUseRecord.setBalance(customerElectronicCard.getAmt());
           customerCardUseRecord.setCreateTime(new Date());
           if (amt != null) {
               customerCardUseRecord.setAmt(amt);
           } else {
               customerCardUseRecord.setAmt(BigDecimal.ZERO);
           }
           customerCardUseRecord.setStationId(Math.toIntExact(stationId));
           customerCardUseRecordMapper.insertCustomerCardUseRecord(customerCardUseRecord);
       }
        return  customerElectronicCard.getId();
    }



    @Override
    @Transactional
    public int updateconsumptionTran(IcRegisteInfo icRegisteInfo, CustomerElectronicCard customerElectronicCard)
    {

        CustomerCardUseRecord customerCardUseRecord = new CustomerCardUseRecord();
        customerCardUseRecord.setOrderNo("");
        customerCardUseRecord.setCustomerNo(customerElectronicCard.getCustomerNo());
        customerCardUseRecord.setUsageType("-");
        customerCardUseRecord.setPayType("stk");
        customerCardUseRecord.setCardOilsType(icRegisteInfo.getCardType());
        customerCardUseRecord.setAmt(icRegisteInfo.getAmt());
        customerCardUseRecord.setBalance(customerElectronicCard.getAmt().subtract(icRegisteInfo.getAmt()));
        customerCardUseRecord.setCreateTime(new Date());
        customerCardUseRecord.setStationId(Integer.parseInt(icRegisteInfo.getStationId() + ""));
        customerCardUseRecord.setStatus("1");
        customerCardUseRecord.setMobilePhone(customerElectronicCard.getMobilePhone());
        customerCardUseRecord.setEnterpriseId(icRegisteInfo.getEnterpriseId());

        this.customerCardUseRecordMapper.insertCustomerCardUseRecord(customerCardUseRecord);

        CustomerElectronicCard newCustomerElectronicCard = new CustomerElectronicCard();
        newCustomerElectronicCard.setId(customerElectronicCard.getId());
        newCustomerElectronicCard.setAmt(customerElectronicCard.getAmt().subtract(icRegisteInfo.getAmt()));
        if(customerElectronicCard.getId() == null){
            return 0;
        }
        // 然后再调用一下sasspay的 createCashOrUnionPayOrder
        return customerElectronicCardMapper.updateCustomerElectronicCard(newCustomerElectronicCard);
//        return icRegisteInfoMapper.updateConsumption(icRegisteInfo);

    }




    public int updatecancellation(IcRegisteInfo icRegisteInfo){

        return icRegisteInfoMapper.updatecancellation(icRegisteInfo);
    }

    /**
     * 批量删除卡编号
     * 
     * @param icCardnos 需要删除的卡编号ID
     * @return 结果
     */
    @Override
    public int deleteIcRegisteInfoByIds(String[] icCardnos)
    {
        return icRegisteInfoMapper.deleteIcRegisteInfoByIds(icCardnos);
    }

    /**
     * 删除卡编号信息
     * 
     * @param icCardno 卡编号ID
     * @return 结果
     */
    @Override
    public int deleteIcRegisteInfoById(String icCardno)
    {
        return icRegisteInfoMapper.deleteIcRegisteInfoById(icCardno);
    }
}
